//
//  SPCSpaceEditTableViewController.swift
//  aj
//
//  Created by leeqb on 1/27/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class SPCSpaceEditTableViewController: AJBaseTableViewController, PhotoPickerDelegate {
    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var coverField: UITextField!
    @IBOutlet weak var descField: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        nameField.layer.borderColor = RGB(182, g: 182, b: 182).CGColor
        coverField.layer.borderColor = RGB(182, g: 182, b: 182).CGColor
        descField.layer.borderColor = RGB(182, g: 182, b: 182).CGColor
    }
    
    //MARK: - Button Clicks
    @IBAction func save(sender: UIButton) {
        let name = nameField.text!
        let cover = coverField.text!
        let desc = descField.text!
        
        if name.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入空间名称")
            return
        }
        
        if cover.isEmpty {
            CBAlertView.shareInstance().showMsg("请选择封面")
            return
        }
        
        if cover.isEmpty {
            CBAlertView.shareInstance().showMsg("请选择空间封面")
            return
        }
        
        let params = ["oper": "1", "albumName": name, "albumUrl": cover, "albumDesc": desc, "albumType": "2"]
        AJNetworkHelper.instance.post("aj_album_mgr", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            print(responseObject)
            if responseObject["returnCode"] as? String == RetCodeSuccess {
                CBAlertView.shareInstance().showMsg("保存成功")
                self.navigationController?.popViewControllerAnimated(true)
            } else {
                CBAlertView.shareInstance().showMsg(responseObject["errorMsg"] as? String)
            }
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                print(error)
            }) { () -> Void in
        }
    }
    
    @IBAction func pickPic(sender: UIButton) {
        PhotoPicker.pick(self)
    }
    
    //MARK: - PhotoPickerDelegate
    func photoPicked(image: UIImage, imagePath: String) {
        coverField.text = imagePath
    }
}
